﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForm_Mes.WarehouseManagement;

namespace WinForm_Mes
{
    public partial class PurchaseOrderView : DevExpress.XtraEditors.XtraForm
    {
        public PurchaseOrderView()
        {
            InitializeComponent();
            this.Load += PurchaseOrderView_Load;
        }

        private async void PurchaseOrderView_Load(object sender, EventArgs e)
        {
            await LoadPurchaseOrdersAsync();
        }
        private void groupControl2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panelControl2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void labelControl2_Click_Custom(object sender, EventArgs e)
        {

        }

        private async void simpleButton2_Click(object sender, EventArgs e)
        {
            // 1. 请求后端获取新采购单号
            string apiUrl = "http://localhost:5113/api/PurchaseOrder/GetCode";
            string newOrderNumber = "";
            // 2. 调用供应商后端接口获取下拉数据
            string supplierApiUrl = "http://localhost:5113/api/Supplier/GetAll";
            List<string> supplierList = new List<string>();
            try
            {
                HttpResponseMessage response = await client.GetAsync(apiUrl);
                response.EnsureSuccessStatusCode();
                string responseContent = await response.Content.ReadAsStringAsync();
                newOrderNumber = responseContent;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"获取采购单号失败: {ex.Message}");
                return;
            }
            //try
            //{
            //    HttpResponseMessage response = await client.GetAsync(apiUrl);
            //    response.EnsureSuccessStatusCode();
            //    string responseContent = await response.Content.ReadAsStringAsync();

            //    // 假设返回 ["供应商A","供应商B","供应商C"]
            //    supplierList = System.Text.Json.JsonSerializer.Deserialize<List<string>>(responseContent);
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show($"获取供应商失败: {ex.Message}");
            //    return;
            //}

            PurchaseOrderAdd addPurchaseOrder = new PurchaseOrderAdd(newOrderNumber, supplierList);

            addPurchaseOrder.ShowDialog();
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("下一页");
        }

        private void PurchaseOrderView_Load_1(object sender, EventArgs e)
        {

        }
    }
    // 用于反序列化采购单号
    public class OrderNumberResponse
    {
        [System.Text.Json.Serialization.JsonPropertyName("orderNumber")]
        public string OrderNumber { get; set; }
    }
    public class Supplier
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }



}